package com.ykdl.member.kid.gears;

import com.umeng.analytics.MobclickAgent;
import com.ykdl.member.KidApp;
import com.ykdl.member.base.QLog;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.advert.AdverltWebView;
import com.ykdl.member.kid.circle.CircleInfoActivity;
import com.ykdl.member.kid.circle.CircleTopicActivity;
import com.ykdl.member.kid.circle.TopicAndResultActivity;
import com.ykdl.member.kid.doctor.ActionsDoctorOnlineActivity;
import com.ykdl.member.kid.doctor.AskDoctorActivity;
import com.ykdl.member.kid.doctor.CampaignInfoWebView;
import com.ykdl.member.kid.doctor.DoctorInfoActivity;
import com.ykdl.member.kid.doctor.DoctorOnlineListActivity;
import com.ykdl.member.kid.doctor.ProblemDetailsAcivity;
import com.ykdl.member.kid.doctor.ToAskDoctorActivity;
import com.ykdl.member.kid.util.ActiveStateUtil;
import com.ykdl.member.kid.widget.dialog.DialogImpl;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

public class BaseFragmentActivity extends FragmentActivity {
	public final String TAG = this.getClass().getSimpleName();
	public Context mContext;
	public ProgressDialog progress;
	public DialogImpl dialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		KidApp.getInstance().getAppManager().addActivity(this);
		this.mContext = this.getApplicationContext();
		 dialog = new DialogImpl();
		 
	}
	public void showProgress(Activity activity){
		dialog = new DialogImpl();
		progress = (ProgressDialog) dialog.showProgressDialog(this, "正在加载中...", new onChannelListener(activity));
	}
	public void dismissProgress(){
		dialog.dismissProgress(progress);
	}

	private class onChannelListener implements DialogInterface.OnCancelListener{
		Activity context;
		public onChannelListener(Activity context){
			this.context = context;
		}
		@Override
		public void onCancel(DialogInterface dialog) {
			if(context instanceof HomeActivity){
//				context.onKeyDown(KeyEvent.KEYCODE_BACK, null);
			}else{
				context.finish();
			}
		}
		
	}
	/**
	 * 退出应用程序
	 * 
	 */
	public void exitApp()
	{
		KidApp.getInstance().getAppManager().AppExit(this, false);
	}
	/**
	 * 设置 出错的提示
	 * @param err  err,错误提示，
	 * @param tip    tip 应该操作
	 */	
	protected void showErrorTip(String err,String tip) {
		findViewById(R.id.errtip_div).setVisibility(View.VISIBLE);
		((TextView)findViewById(R.id.tv_error_text)).setText(err);
		((TextView)findViewById(R.id.tv_error_text2)).setText(tip);
	}
	
	/**
	 * 隐藏 出错的提示
	 */	
	protected void hidErrorTip( ) {
		findViewById(R.id.errtip_div).setVisibility(View.GONE);		
	}
	/**
	 * 退出应用程序，且在后台运行
	 * 
	 */
	public void exitAppToBackground()
	{
		KidApp.getInstance().getAppManager().AppExit(this, true);
	}
	/**
	 * 根据action 跳转到到不同的页面，主要是展位在使用
	 * @param paction
	 * @param pbean
	 */
	public void goAction(String paction) {
		if(paction==null){
			return;
		}


		//进圈子
		//lmyr://subject/10/786326
	    //"http://tool.test.com/group/1",
		 //进帖子
	    //"http://tool.test.com/subject/1/2",
		//问医生
	    //"http://docask/1",
		 //医生专题
	    //"http://tool.test.com/docspec/1/asfafa%32%32%32%32adsfas",
		//普通专题
	    //"http://tool.test.com/hotspec/1/asfadsfasdfasf",
		
		if(TextUtils.isEmpty(paction))return;
		if (paction.startsWith("http:")) {
			Intent intent = new Intent(mContext,AdverltWebView.class);
			intent.putExtra(KidAction.INTENT_URL, paction+"");
			startActivity(intent);
		}else {
			String[] end = paction.split("\\//")[1].split("\\/");
			Intent intent = null;
			 if (paction.contains("subject")) {//进帖子详情
				 intent =new Intent(mContext,TopicAndResultActivity.class);
				 intent.putExtra(TopicAndResultActivity.STR_TOPIC_ID,end[2]);
				 intent.putExtra(TopicAndResultActivity.STR_GROUP_ID, end[1]);
			} else if (paction.contains("group")) {//进圈子
				intent = new Intent(mContext, CircleTopicActivity.class);
				intent.putExtra(TopicAndResultActivity.STR_GROUP_ID, end[1]);
			} else if (paction.contains("docask")){//医生
				intent = new Intent(mContext, ToAskDoctorActivity.class);
				intent.putExtra("campaign_id", end[1]);
			} else if (paction.contains("docspec")){//医生专题
				intent = new Intent(mContext, ProblemDetailsAcivity.class);
				intent.putExtra(KidAction.DOCTOR_TOPICI_DD, end[1]);
			} else if(paction.contains("hotspec")){//普通专题
				intent = new Intent(mContext, TopicsSheetListActivity.class);
				intent.putExtra("catalog_id", end[1]);
			}else if(paction.contains("docintro")){//医生简介
				intent = new Intent(mContext, DoctorInfoActivity.class);
					intent.putExtra("doctor_id", end[1]);
			}else if(paction.contains("profession_topic")){//专家坐诊帖子页 
				intent = new Intent(mContext, ProblemDetailsAcivity.class);
				intent.putExtra(KidAction.DOCTOR_TOPICI_DD, end[2]);
		}else if(paction.contains("profession")){//专家坐诊活动详情页
			if (end[2].equals("0")) {
				intent = new Intent(mContext,CampaignInfoWebView.class);
				intent.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, end[1]);
			}else if(end[2].equals("1")){
				intent = new Intent(mContext, ActionsDoctorOnlineActivity.class);
				intent.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, end[1]);
			}else if(end[2].equals("2")){
				intent = new Intent(mContext, ActionsDoctorOnlineActivity.class);
				intent.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, end[1]);
				intent.putExtra(KidAction.IS_GONE_BT, false);
			}
		}else if(paction.contains("docol")){//医生在线
				dialog = new DialogImpl();
				progress = (ProgressDialog) dialog.showProgressDialog(this, "请稍等...", new onChannelListener(this));
				ActiveStateUtil.getInstance(this).gogoActionsDoctorOnlineByStatus(end[2], progress);
			}
			 if(intent ==null)return;
			 startActivity(intent);
			}
		}
		
	
	
	public void goActionsDoctorOnlineActivity(int status, String doctor_campaign_id){
		Intent  intent = null;
		if (status==0) {
			intent = new Intent(mContext,CampaignInfoWebView.class);
			intent.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, doctor_campaign_id);
		}else if(status ==1){
			intent = new Intent(mContext,ActionsDoctorOnlineActivity.class);
			intent.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, doctor_campaign_id);
		}else if(status ==2){
			intent = new Intent(mContext,ActionsDoctorOnlineActivity.class);
			intent.putExtra(KidAction.DOCTOR_CAMPAIGN_ID, doctor_campaign_id);
			intent.putExtra(KidAction.IS_GONE_BT, false);
		}
		if(intent ==null)return;
		 startActivity(intent);
		 return;
	}
	public static boolean isConnect() {
		// 获取手机所有连接管理对象（包括对wi-fi,net等连接的管理）
		try {
			ConnectivityManager connectivity = (ConnectivityManager)KidApp.getInstance().getApplicationContext()
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			if (connectivity != null) {
				// 获取网络连接管理的对象
				NetworkInfo info = connectivity.getActiveNetworkInfo();
				if (info != null && info.isConnected()) {
					// 判断当前网络是否已经连接
					// if (info.getState() == NetworkInfo.State.CONNECTED) {
					return true;
					// }
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			QLog.debug("error", e.toString());
		}
		return false;
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		MobclickAgent.onPageEnd("SplashScreen"); 
		 MobclickAgent.onPause(this);
		super.onPause();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		MobclickAgent.onPageStart("SplashScreen");
		 MobclickAgent.onResume(this);   
		super.onResume();
	}
	
}
